import {
	baseUrl
} from "@/config/baseUrl.js"
const req = obj => {
	uni.showLoading({
		title: "加载中...",
		mask: true,
	})
	return new Promise((resolve, reject) => {
		let url = baseUrl + obj.url;
		let method = obj.method || "GET";
		let data = obj.data || {};
		let header = obj.header || {
			'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : '',
			'Content-Type': obj.contentType || 'application/json;charset=UTF-8',
		};
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			dataType: 'json',
			success: (res => {
				if (res.data.code == 1 || res.data.code == 200) {
					resolve(res.data)
				} else if(res.data.code == 50010 || res.data.code == 401) {
					// uni.showModal({
					// 	title: "温馨提示",
					// 	content: res.data.msg,
					// 	showCancel: false,
					// })
					// reject(res.data.msg);
					uni.showToast({
						title: "未登录！",
						icon: "error"
					})
					setTimeout(() => {
						uni.clearStorageSync()
						uni.reLaunch({
							url: "/pages/login/login"
						})
					},1500)
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.msg,
						duration: 2000,
					})
					reject(res.data.msg)
					// setTimeout(() => {
					// 	uni.navigateBack()
					// },1500)
				}
			}),
			fail: ((err) => {
				uni.showToast({
					icon: "none",
					title: "请求错误",
					duration: 2000,
				})
				reject(err.data)
			}),
			complete: () => {
				uni.hideLoading()
			}
		})

	})
}
export default req
